package com.example.course.converter;

import com.example.course.bean.Classes;
import com.example.course.bean.Student;
import com.example.course.dto.StudentDTO;
import com.example.course.service.ClassesService;
import com.example.course.vo.LoginVO;
import com.example.course.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 学生实体与DTO/VO转换器
 *
 * @author Jasmine-drs
 * @date 2025/4/22 16:30
 */
@Component
public class StudentConverter {

	private final ClassesService classesService;

	public StudentConverter(@Lazy ClassesService classesService) {
		this.classesService = classesService;
	}

	/**
	 * 将StudentDTO转换为Student实体
	 *
	 * @param dto StudentDTO
	 * @return Student
	 */
	public Student toEntity(StudentDTO dto) {
		if (dto == null) {
			return null;
		}
		Student entity = new Student();
		BeanUtils.copyProperties(dto, entity);
		return entity;
	}

	/**
	 * 将Student实体转换为StudentVO
	 *
	 * @param entity Student
	 * @return StudentVO
	 */
	public StudentVO toVO(Student entity) {
		if (entity == null) {
			return null;
		}

		StudentVO vo = new StudentVO();
		BeanUtils.copyProperties(entity, vo);

		// 设置性别文本
		vo.setSexText(entity.getSex() != null && entity.getSex() == 1 ? "女" : "男");

		// 设置班级名称
		if (entity.getClassesId() != null) {
			Classes classes = classesService.getById(entity.getClassesId());
			if (classes != null) {
				vo.setClassName(classes.getName());
			}
		}

		return vo;
	}

	/**
	 * 将Student实体转换为LoginVO
	 *
	 * @param entity Student
	 * @return LoginVO
	 */
	public LoginVO toLoginVO(Student entity) {
		if (entity == null) {
			return null;
		}

		LoginVO vo = new LoginVO();
		BeanUtils.copyProperties(entity, vo);

		// 设置性别文本
		vo.setSexText(entity.getSex() != null && entity.getSex() == 1 ? "女" : "男");

		// 设置班级名称
		if (entity.getClassesId() != null) {
			Classes classes = classesService.getById(entity.getClassesId());
			if (classes != null) {
				vo.setClassName(classes.getName());
			}
		}

		// 设置登录时间
		vo.setLoginTime(LocalDateTime.now());

		return vo;
	}

	/**
	 * 将Student实体列表转换为StudentVO列表，并批量查询班级信息
	 *
	 * @param entityList Student列表
	 * @return StudentVO列表
	 */
	public List<StudentVO> toVOList(List<Student> entityList) {
		if (entityList == null || entityList.isEmpty()) {
			return new ArrayList<>();
		}

		// 收集所有班级ID
		List<Integer> classesIds = entityList.stream()
				.map(Student::getClassesId)
				.filter(Objects::nonNull)
				.distinct()
				.collect(Collectors.toList());

		// 批量查询班级
		Map<Integer, Classes> classesMap = classesService.listByIds(classesIds).stream()
				.collect(Collectors.toMap(Classes::getId, Function.identity()));

		// 转换为VO
		return entityList.stream().map(entity -> {
			StudentVO vo = new StudentVO();
			BeanUtils.copyProperties(entity, vo);

			// 设置性别文本
			vo.setSexText(entity.getSex() != null && entity.getSex() == 1 ? "女" : "男");

			// 设置班级名称
			if (entity.getClassesId() != null) {
				Classes classes = classesMap.get(entity.getClassesId());
				if (classes != null) {
					vo.setClassName(classes.getName());
				}
			}

			return vo;
		}).collect(Collectors.toList());
	}
} 